package com.example.yingjiguanli_demo.utils;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.yingjiguanli_demo.config.SessionManager;
import com.example.yingjiguanli_demo.mapper.UsersMapper;
import com.example.yingjiguanli_demo.pojo.Users;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class UserUtils {
    @Autowired
    private SessionManager sessionManager;
    @Autowired
    private UsersMapper userMapper;
    public int getUserId(String sessionId){
        String token = sessionManager.getSession(sessionId);
        Claims claims = JwtUtils.parseJWT(token);
        String username = JwtUtils.getUserName(claims);
        QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        Users user = userMapper.selectOne(queryWrapper);
        return Math.toIntExact(user.getId());
    }

    public String getUserName(String sessionId){
        String token = sessionManager.getSession(sessionId);
        Claims claims = JwtUtils.parseJWT(token);
        return JwtUtils.getUserName(claims);
    }

}
